package com.ruoyi.oto.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.constants.*;
import com.ruoyi.oto.domain.*;
import com.ruoyi.oto.domain.dto.CheckDemandsDTO;
import com.ruoyi.oto.domain.dto.DemandsCompanyDTO;
import com.ruoyi.oto.mapper.DemandsDepartMapper;
import com.ruoyi.oto.mapper.DemandsRecordMapper;
import com.ruoyi.oto.mapper.DemandsReplyMapper;
import com.ruoyi.oto.service.BranchLinkService;
import com.ruoyi.oto.service.CompanyCoreService;
import com.ruoyi.oto.service.DemandsDepartService;
import com.ruoyi.oto.service.DemandsRecordService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 诉求部门处理服务
 *
 * @author ruoyi
 */
@Service
public class DemandsDepartServiceImpl extends ServiceImpl<DemandsDepartMapper, DemandsDepart> implements DemandsDepartService
{
    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private DemandsReplyMapper demandsReplyMapper;
    @Autowired
    private BranchLinkService branchLinkService;

    /**
     * 增加诉求部门关系记录
     * @param loginUser
     * @param demandsRecord
     * @param branchId
     * @param upBranchId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDemandsDepart(LoginUser loginUser, DemandsRecord demandsRecord, Long branchId, Long upBranchId) {
        DemandsDepart.DemandsDepartBuilder builder = DemandsDepart.builder().demandsId(demandsRecord.getId()).branchId(branchId).upBranchId(upBranchId);
        SysDept sysDept = sysDeptService.selectDeptById(branchId);
        if(ObjUtil.isNotNull(sysDept)){
            builder.branchName(sysDept.getDeptName());
        }
        builder.upBranchId(upBranchId);
        sysDept = sysDeptService.selectDeptById(upBranchId);
        if(ObjUtil.isNotNull(sysDept)){
            builder.upBranchName(sysDept.getDeptName());
        }
        //获取branchId的联企人员
        List<BranchLink> branchLinks = branchLinkService.list(new LambdaQueryWrapper<BranchLink>().eq(BranchLink::getBranchId, branchId).eq(BranchLink::getCompanyId,demandsRecord.getCompanyId()).eq(BranchLink::getDelFlag, DeleteFlag.NO));
        if(ObjUtil.isNull(branchLinks)||branchLinks.isEmpty()){
            return;
        }
        DemandsDepart demandsDepart = builder.demandsTitle(demandsRecord.getTitle()).demandsContent(demandsRecord.getContent()).images(demandsRecord.getImages()).status(DemandsDepartStatus.未处置).
                companyId(demandsRecord.getCompanyId()).companyName(demandsRecord.getCompanyName()).ysStatus(YsOpinion.未受理)
                .build();
        if(loginUser.getMember()!=null){
            demandsDepart.setCreateBy(loginUser.getMember().getUsername());
        }
        else{
            demandsDepart.setCreateBy(loginUser.getUser().getUserName());
        }
        demandsDepart.setCreateTime(new Date());
        demandsDepart.insert();
    }
    /**
     * 增加诉求纪检部门的关系记录
     * @param demandsRecord
     * @param branchId
     * @param upBranchId
     */
    @Override
    public void addDemandsDepartToJw(DemandsRecord demandsRecord, Long branchId, Long upBranchId) {
        DemandsDepart.DemandsDepartBuilder builder = DemandsDepart.builder().demandsId(demandsRecord.getId()).branchId(branchId);
        SysDept sysDept = sysDeptService.selectDeptById(branchId);
        if(ObjUtil.isNotNull(sysDept)){
            builder.branchName(sysDept.getDeptName());
        }
        DemandsDepart demandsDepart = builder.demandsTitle(demandsRecord.getTitle()).demandsContent(demandsRecord.getContent()).images(demandsRecord.getImages()).status(DemandsDepartStatus.未处置).
                companyId(demandsRecord.getCompanyId()).companyName(demandsRecord.getCompanyName()).build();
        demandsDepart.setCreateBy("system");
        demandsDepart.setCreateTime(new Date());
        demandsDepart.insert();
    }

    /**
     * 获取诉求处置的部门
     * @param demandsId
     * @return
     */
    @Override
    public List<DemandsDepart> getDemandsDepartList(Long demandsId) {
        return this.getBaseMapper().getDemandsDepartByDemandsId(demandsId);
    }

    @Override
    public DemandsReply getDemandsReply(Long replyId) {
        return demandsReplyMapper.selectById(replyId);
    }

    @Override
    public List<DemandsReply> getDemandsReplyByDemandDepartId(Long demandsId, Long replyId) {
        return demandsReplyMapper.getDemandsReplyByDemandDepartId(demandsId, replyId);
    }
}
